package com.wu.llpsw.event;

import com.wu.llpsw.data.levelData;
import com.wu.llpsw.data.playerData;
import com.wu.llpsw.key.keyRegister;
import com.wu.llpsw.networking.ModMessage;
import com.wu.llpsw.networking.packet.C2SPacket;
import com.wu.llpsw.networking.packet.huiFuYuanLi;
import com.wu.llpsw.tool.select;
import net.minecraft.client.Minecraft;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.List;

/**
 * 按键事件,只运行在客户端上
 */
@Mod.EventBusSubscriber(modid = "wu",value = Dist.CLIENT)
@OnlyIn(Dist.CLIENT)
public class key {
    public static final keyRegister KEY=new keyRegister();
    private static boolean zibaoLock=false;//自爆键是否被按下
    //private  static int huiFuTime=0;



    //按下按键的瞬间
    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public static void onKeyDownTime(InputEvent.KeyInputEvent event){
        Player player=Minecraft.getInstance().player;
        Level world = player != null ? player.getCommandSenderWorld() : null;
        if(player!=null&&world!=null){
            if(player.isOnGround()){
                //大跳
                if(KEY.jump.isDown()){
                    double x=player.getLookAngle().x;
                    double y=player.getLookAngle().y;
                    double z=player.getLookAngle().z;
                    if (y<=player.getY()){
                        y=0.8d;
                    }
                    player.setDeltaMovement(x*2.50d,y*1.5d,z*2.50d);
                }

            }

            //自爆
            if(KEY.zibao.isDown()&!zibaoLock){
                int range=levelData.maxRangeValue[levelData.getLevel(player.experienceLevel)];
                zibaoLock=true;
                //产生爆炸
                world.explode(player,player.getX(),player.getY()+1,player.getZ(),0, Explosion.BlockInteraction.BREAK);
                List<LivingEntity> entities= select.getEntitiesAround(world,player.getX(),player.getY(),player.getZ(),range);
                List<Player> playerList=select.getAroundPlayer(world,player.getX(),player.getY(),player.getZ(),range);
                //对周围的生物造成伤害
                for (LivingEntity entity : entities) {
                    entity.hurt(DamageSource.GENERIC, (float) playerData.getAttack(player)*10.0f);
                }
                //对周围的玩家造成伤害
                for (Player entity : playerList) {
                    entity.hurt(DamageSource.GENERIC, (float) playerData.getAttack(player)*5.0f);
                }
            }
            else if(!KEY.zibao.isDown()&zibaoLock){
                zibaoLock=false;
            }

        }
    }
}
